package com.yhm.testSpringMvc.Thread;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadLocalDemo {
    public static void main(String[] args) {

        ExecutorService executorService  = Executors.newFixedThreadPool(3);
        for( int i=0;i<20;i++ ) {
            executorService.execute(new yhmThreadPoolSimpleDemo1());
        }
        executorService.shutdown();
        System.out.println("主线程执行完毕");

    }

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
    static class yhmThreadPoolSimpleDemo1 implements Runnable {

        @Override
        public void run() {
            try {
                int random = new Random().nextInt();
                System.out.println(Thread.currentThread().getName() + " 放入随机整数" + random + "");

                threadLocal.set(random + "");
                Thread.sleep(new Random().nextInt(999));
                System.out.println(Thread.currentThread().getName() + " 取出随机整数" + threadLocal.get() + "");

                //做其他事情
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
